package cn.tedu._04mvcweibo.controller;

import cn.tedu._04mvcweibo.common.response.JsonResult;
import cn.tedu._04mvcweibo.common.response.StatusCode;
import cn.tedu._04mvcweibo.mapper.WeiboMapper;
import cn.tedu._04mvcweibo.pojo.dto.WeiboDTO;
import cn.tedu._04mvcweibo.pojo.entity.Weibo;
import cn.tedu._04mvcweibo.pojo.vo.UserVO;
import cn.tedu._04mvcweibo.pojo.vo.WeiboDetailVO;
import cn.tedu._04mvcweibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * Validated注解: 用于非POJO参数校验
 */
@Validated
@Api(tags = "02.微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {

    //自动装配
    @Autowired(required = false)
    private WeiboMapper weiboMapper;

    /**
     * 发布微博功能
     */
    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.debug("weiboDTO = " + weiboDTO);
        /*
            1.校验登录状态
              1.1 未登录: 直接返回2
              1.1 登录状态: 接口方法[将数据插入数据表]
         */
         UserVO userVO = (UserVO) session.getAttribute("user");
//        //通过判断 userVO是否为null来判断登录状态
//        if (userVO == null){
//            //未登录
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        // 登录状态,发微博[实体类]

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }
    /**
     * 微博首页列表
     */
    @ApiOperation(value = "微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult<WeiboIndexVO> selectIndex(){
        /*
            无需校验用户登录状态
            直接调用接口方法查询数据即可
         */
        List<WeiboIndexVO> list = weiboMapper.selectIndex();

        return JsonResult.ok(list);
    }

    /**
     * 微博详情页
     */
    @ApiOperation(value = "微博详情页功能")
    @GetMapping("selectById")
    @ApiImplicitParams(value={
            @ApiImplicitParam(name = "id", value = "微博ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string")
    })
    // http://localhost:8080/v1/weibo/selectById?aaa=200
    public JsonResult selectById(@Range(min = 1, max = 300, message = "微博ID必须在1-300之间") @RequestParam int id, String username){
        /*
            无需校验用户登录状态
            直接调用查询接口返回数据
         */
        if (id < 1){
            //主动抛出异常
            throw new IllegalArgumentException("ID值不能小于1");
        }

        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);

        return JsonResult.ok(weiboDetailVO);
    }
}







